package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonPanel extends JPanel {

    private JButton enterBtn;
    private JButton rePlayBtn;
    private JButton exitBtn;
    private Integer num = 0;

    public ButtonPanel(HiLoFrame hlFrame, UpPanel upPanel, MidPanel midP) {

        this.enterBtn = new JButton("确定");
        this.rePlayBtn = new JButton("重玩");
        this.rePlayBtn.setEnabled(false);

        this.exitBtn = new JButton("退出");
        System.out.println(hlFrame.getRandomNum());
        this.enterBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton jButton = (JButton) e.getSource();
                if (num < 6) {
                    if (upPanel.getInputTxt().getText().matches("[5-9][0-9]")) {
                        num++;
                        if (jButton.getText().equals(enterBtn.getText())) {
                            if (hlFrame.getRandomNum() > Integer.parseInt(upPanel.getInputTxt().getText())) {
                                midP.getMsgLab().setText("猜小了，你还有" + (7 - num) + "次机会");
                            } else if (hlFrame.getRandomNum() < Integer.parseInt(upPanel.getInputTxt().getText())) {
                                midP.getMsgLab().setText("猜大了，你还有" + (7 - num) + "次机会");
                            } else {
                                midP.getMsgLab().setText("恭喜你猜对了");
                                num = 0;
                                enterBtn.setEnabled(false);
                            }
                        }
                    } else {
                        midP.getMsgLab().setText("请输入55-99");
                    }
                } else {
                    midP.getMsgLab().setText("你个挫人，没有机会了");
                    rePlayBtn.setEnabled(true);
                    enterBtn.setEnabled(false);
                }
            }
        });
        this.add(this.enterBtn);
        this.add(this.rePlayBtn);
        this.add(this.exitBtn);
    }

    public JButton getEnterBtn() {
        return enterBtn;
    }

    public void setEnterBtn(JButton enterBtn) {
        this.enterBtn = enterBtn;
    }

    public JButton getRePlayBtn() {
        return rePlayBtn;
    }

    public void setRePlayBtn(JButton rePlayBtn) {
        this.rePlayBtn = rePlayBtn;
    }

    public JButton getExitBtn() {
        return exitBtn;
    }

    public void setExitBtn(JButton exitBtn) {
        this.exitBtn = exitBtn;
    }
}
